import { NgFor, NgIf } from '@angular/common';
import { Component, ElementRef } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { NzButtonComponent, NzButtonModule } from 'ng-zorro-antd/button';
import { NzDividerModule } from 'ng-zorro-antd/divider';
import { NzDropDownModule } from 'ng-zorro-antd/dropdown';
import { NzEmptyModule } from 'ng-zorro-antd/empty';
import { NzFormModule } from 'ng-zorro-antd/form';
import { NzInputModule } from 'ng-zorro-antd/input';
import { NzMenuModule } from 'ng-zorro-antd/menu';
import { NzModalModule } from 'ng-zorro-antd/modal';
import { NzTableModule, NzTrDirective } from 'ng-zorro-antd/table';
import { Column, ProductSummary } from '../../type';
import { CustomBaseComponent } from '../custom-base/custom-base.component';
import { FieldSelectorComponent } from '../field-selector/field-selector.component';
import { ProductSummaryService, TcService } from '../tc.service';

// let tcService: TcService = new TcService();

@Component({
  selector: 'app-material-summary',
  standalone: true,
  imports: [
    NzTableModule, NzTrDirective, NgFor, NzDividerModule, NzFormModule, NzButtonComponent,
    NzInputModule, NzModalModule, NgIf, NzButtonModule, FormsModule, NzMenuModule, NzDropDownModule,
    NzModalModule, FieldSelectorComponent, NzEmptyModule

  ],
  templateUrl: '../custom-base/custom-base.component.html',
  styleUrl: '../custom-base/custom-base.component.less'
})
export class ProductSummaryComponent extends CustomBaseComponent<ProductSummary> {

  getExportUrl(): string {
    let filter = this.service.filter;//`rank_desc = 1`;
    return `/export-excel/export/海关备案产品汇总表.xlsx?filter=${encodeURIComponent(filter)}`;
  }

  getTreeField(): keyof ProductSummary {
    return 'item_id';
  }

  constructor(elementRef: ElementRef, public service: ProductSummaryService) {
    super(elementRef);

  }

  getColumns(): Column<ProductSummary>[] {
    let columns: Column<ProductSummary>[] = [
      { title: '编号', field: 'item_id' },
      { title: '版本', field: 'item_revision_id', width: '60px' },
      { title: '名称', field: 'object_name' },
      { title: '所有者', field: 'owning_user' },
      { title: '上次修改用户', field: 'last_mod_user' },
      { title: '修改日期', field: 'last_mod_date' },
      { title: '创建日期', field: 'creation_date' },
      { title: '描述', field: 'object_desc' },
      { title: '发布状态', field: 'release_status_list' },
      { title: '发布日期', field: 'date_released' },
      { title: '大类', field: 'big_classname' },
      { title: '备案项目名称', field: 'project_name' },
      { title: '用途', field: 'usage' },
      { title: '出货颜色', field: 'shipment_color' }, // to complete
      { title: '最终成品编码', field: 'projectcode_id_last' },
      { title: '成品尺寸', field: 'project_size_yc' },
      { title: '几D(有无含胶片)', field: 'isnull_D' },
      { title: 'LOGO', field: 'logo' },
      { title: '品牌', field: 'brand' },
      { title: '出货工厂', field: 'product_shipment' },
      { title: '出货方式', field: 'shipping_method' },
      { title: '适用机型', field: 'compatible' },
      { title: '营业电话', field: 'business_tel' },
      { title: '营业项目组', field: 'business_project' },
      { title: '产品BOM编号(CRR)', field: 'project_line_BOM' },
      { title: '原料材质', field: 'rawmaterial_texture' },
      { title: '白片单重', field: 'first_weight' },
      { title: '成品尺寸mm', field: 'project_size_mm' },
      { title: '成品单片净重', field: 'project_weight' },
      { title: '平方米', field: 'square_metres' },
      { title: '成品视窗', field: 'project_windows' },
      { title: '型号', field: 'model' },
      { title: '主要组成部分', field: 'key_component' },
      { title: '物流园（清关关区）', field: 'logistics_park' },
      { title: '生产流程', field: 'work_flow' },
      { title: '备注', field: 'remark' },
    ];

    return columns;
  }
  getDefaultSearchFields(): (keyof ProductSummary)[] {
    return ['big_classname', 'project_name', 'usage', 'projectcode_id_last', 'project_size_yc'];
  }
  getLatestDataItems(): Promise<ProductSummary[]> {
    return this.service.getLatestProductSummary();
  }
  searchDataItems(searchText: string, searchFields: (keyof ProductSummary)[]): Promise<ProductSummary[]> {
    return this.service.searchProductSummary(searchText, searchFields);
  }

  getHistoryDataItems(dataItem: ProductSummary): Promise<ProductSummary[]> {
    return this.service.getHistoryProductSummary([dataItem.item_id]);
  }

}

// import { NgFor, NgIf } from '@angular/common';
// import { Component, ElementRef, ViewChild } from '@angular/core';
// import { NzTableModule, NzTrDirective } from 'ng-zorro-antd/table';
// import { NzDividerModule } from "ng-zorro-antd/divider";
// import { NzFormModule } from "ng-zorro-antd/form";
// import { NzInputModule } from "ng-zorro-antd/input";
// import { NzModalModule } from 'ng-zorro-antd/modal';
// import { NzButtonModule, NzButtonComponent } from 'ng-zorro-antd/button';
// import type { Column, ProductSummary } from '../../type';
// import { TcService } from '../tc.service';
// import { FormsModule } from '@angular/forms';
// import { NzMenuModule } from 'ng-zorro-antd/menu';
// import { NzDropDownModule } from 'ng-zorro-antd/dropdown';
// import { FieldSelectorComponent } from '../field-selector/field-selector.component';

// @Component({
//   selector: 'app-product-summary',
//   standalone: true,
//   imports: [
//     NzTableModule, NzTrDirective, NgFor, NzDividerModule, NzFormModule, NzButtonComponent,
//     NzInputModule, NzModalModule, NgIf, NzButtonModule, FormsModule, NzMenuModule, NzDropDownModule,
//     NzModalModule, FieldSelectorComponent
//   ],
//   templateUrl: './product-summary.component.html',
//   styleUrl: './product-summary.component.less'
// })
// export class ProductSummaryComponent {



//   isFieldsDialogVisible: boolean = false;
//   dataItems: ProductSummary[] = [];
//   expands: { [itemId: string]: boolean } = {};
//   searchText = '';
//   isSearching = false;
//   tableHeight = window.innerHeight - 180;
//   searchFields: (keyof ProductSummary)[] = ProductSummaryComponent.loadSearchFields();

//   @ViewChild('dialog') dialog!: FieldSelectorComponent<ProductSummary>;

//   columns: Column<ProductSummary>[] = [
//     { title: '编号', field: 'item_id' },
//     { title: '版本', field: 'item_revision_id', width: '60px' },
//     { title: '名称', field: 'object_name' },
//     { title: '所有者', field: 'owning_user' },
//     { title: '上次修改用户', field: 'last_mod_user' },
//     { title: '修改日期', field: 'last_mod_date' },
//     { title: '创建日期', field: 'creation_date' },
//     { title: '描述', field: 'object_desc' },
//     { title: '发布状态', field: 'release_status_list' },
//     { title: '发布日期', field: 'date_released' },
//     { title: '大类', field: 'big_classname' },
//     { title: '备案项目名称', field: 'project_name' },
//     { title: '用途', field: 'usage' },
//     { title: '出货颜色', field: 'shipment_color' }, // to complete
//     { title: '最终成品编码', field: 'projectcode_id_last' },
//     { title: '成品尺寸', field: 'project_size_yc' },
//     { title: '几D(有无含胶片)', field: 'isnull_D' },
//     { title: 'LOGO', field: 'logo' },
//     { title: '品牌', field: 'brand' },
//     { title: '出货工厂', field: 'product_shipment' },
//     { title: '出货方式', field: 'shipping_method' },
//     { title: '适用机型', field: 'compatible' },
//     { title: '营业电话', field: 'business_tel' },
//     { title: '营业项目组', field: 'business_project' },
//     { title: '产品BOM编号(CRR)', field: 'project_line_BOM' },
//     { title: '原料材质', field: 'rawmaterial_texture' },
//     { title: '白片单重', field: 'first_weight' },
//     { title: '成品尺寸mm', field: 'project_size_mm' },
//     { title: '成品单片净重', field: 'project_weight' },
//     { title: '平方米', field: 'square_metres' },
//     { title: '成品视窗', field: 'project_windows' },
//     { title: '型号', field: 'model' },
//     { title: '主要组成部分', field: 'key_component' },
//     { title: '物流园（清关关区）', field: 'logistics_park' },
//     { title: '生产流程', field: 'work_flow' },
//     { title: '备注', field: 'remark' },
//   ];

//   constructor(private tcService: TcService, private elementRef: ElementRef) {
//     tcService.getLatestProductSummary().then(r => {
//       this.dataItems = r;
//     })

//     debugger;

//   }

//   async search() {
//     this.isSearching = true;
//     this.tcService.search(this.searchText, this.searchFields)
//       .then(r => {
//         this.dataItems = r;
//       })
//       .finally(() => {
//         this.isSearching = false;
//       })
//   }

//   async onSearchInputKeydown(event: KeyboardEvent) {
//     if (event.key == 'Enter') {
//       this.search();
//     }
//   }

//   async loadHistor(item: ProductSummary, isExpand: boolean) {
//     this.expands[item.item_id] = isExpand;
//     if (item.children) {
//       return;
//     }

//     let productSummaries = await this.tcService.getHistoryProductSummary([item.item_id]);
//     item.children = productSummaries;
//     this.dataItems = JSON.parse(JSON.stringify(this.dataItems));
//   }

//   hideFieldsDialog() {
//     this.isFieldsDialogVisible = false;
//   }

//   showFieldsDialog() {
//     this.dialog.show(this.searchFields, (selectedFields) => {

//       while (this.searchFields.length > 0)
//         this.searchFields.pop();

//       this.searchFields.push(...selectedFields);
//       ProductSummaryComponent.saveSearchFields(this.searchFields);
//     });
//   }

//   onFieldChanged(event: Event, field: keyof ProductSummary) {
//     let input = event.target as HTMLInputElement;
//     if (input.checked) {
//       this.searchFields.push(field);
//     }
//     else {
//       if (this.searchFields.length == 1) {
//         //TODO: 至少保留一项 显示错误信息
//         return;
//       }
//       this.searchFields = this.searchFields.filter(O => O != field);
//     }
//   }

//   getSearchInputPlaceHolderText() {
//     let names = this.columns.filter(o => this.searchFields.indexOf(o.field) >= 0).map(o => o.title).join('、');
//     return `请输入${names}，按回车进行搜索`;
//   }

//   static loadSearchFields() {
//     let text = localStorage.getItem('product-summary-search-fields');
//     let searchFields: (keyof ProductSummary)[];
//     if (!text) {
//       searchFields = ['big_classname', 'project_name', 'usage', 'projectcode_id_last', 'project_size_yc'];
//       text = JSON.stringify(searchFields);
//       localStorage.setItem('product-summary-search-fields', text);
//     }
//     else {
//       searchFields = JSON.parse(text);
//     }

//     return searchFields;
//   }

//   static saveSearchFields(fields: (keyof ProductSummary)[]) {
//     let text = JSON.stringify(fields);
//     localStorage.setItem('product-summary-search-fields', text);
//   }


// }
